package dialog;

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;

import model.Layer;
import utils.ImageOperationUtils;
import utils.ImageUtils;
import abstraction.ImageOperationDialog;

/**
 * 
 * @author Maxime
 */
public class ColorOperationsFilterDialog extends ImageOperationDialog implements
		ItemListener {

	private final String lookupNames[] = { "Normal", "Invert", "Invert Red",
			"Invert Red and Green", "Posterized", "Threshold",
			"Black and White" };

	private JComboBox m_lookupList;
	private short[][] m_colorChange;

	public ColorOperationsFilterDialog(JFrame parent, Layer layer) {
		super(parent, layer.getImage());
		m_titleLabel.setText("Pixel Operations");
		m_componentPanel.setLayout(new GridLayout(1, 1));
		m_lookupList = new JComboBox(lookupNames);
		m_componentPanel.add(m_lookupList);
		m_lookupList.addItemListener(this);

	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {

		//black and white
		if (m_lookupList.getSelectedIndex() == 6) {
			m_preview = ImageOperationUtils.createBlackAndWhite(m_current);
		} else {

			short[] noColor = new short[256];
			short[] inverted = new short[256];
			short[] posterized = new short[256];
			short[] threshold = new short[256];
			short[] normal = new short[256];

			for (int i = 0; i < 256; i++) {
				normal[i] = (short) i;
			}

			// make an inverted lookup table
			for (int i = 0; i < 256; i++) {
				inverted[i] = (short) (255 - i);
			}

			// make a posterize loopup table
			for (int i = 0; i < 256; i++) {
				posterized[i] = (short) (i - (i % 64)); // changing this value
														// alters the effect
			}

			// make a threshold loopup table
			for (int i = 0; i < 256; i++) {
				if (i < 128) // changing this value alters the effect
				{
					threshold[i] = (short) 0;
				} else {
					threshold[i] = (short) 255;
				}
			}
			switch (m_lookupList.getSelectedIndex()) {
			case 0: {
				m_colorChange = new short[][] { normal, normal, normal, normal };
				break;
			}
			case 1: {
				m_colorChange = new short[][] { inverted, inverted, inverted,
						normal };
				break;
			}
			case 2: {
				m_colorChange = new short[][] { inverted, noColor, noColor,
						normal };
				break;
			}
			case 3: {
				m_colorChange = new short[][] { inverted, inverted, noColor,
						normal };
				break;
			}
			case 4: {
				m_colorChange = new short[][] { posterized, posterized,
						posterized, normal };
				break;
			}
			case 5: {
				m_colorChange = new short[][] { threshold, threshold,
						threshold, normal };
				break;
			}
			}

			m_preview = ImageOperationUtils.createLookupOp(m_current,
					m_colorChange);
		}

		m_imageLabel.setIcon(new ImageIcon(ImageUtils.resizeTo(m_preview,
				m_newWidth, m_newHeight)));
	}

}
